/**
 * gislin 2017年9月19日
 */
package test.db;

import com.btcode.common.MyLog;
import com.btcode.db.config.DataBaseConfig;
import com.btcode.db.config.DataBaseType;
import com.btcode.db.executer.DefaultDataExecuterFactory;
import com.btcode.db.executer.IDataExecuter;
import com.btcode.db.executer.IDataExecuterFactory;
import com.btcode.orm.cache.EhcacheExecuter;
import com.btcode.orm.cache.IORMCacheExecuter;
import com.btcode.orm.conver.DefaultModelConver;
import com.btcode.orm.conver.DefaultValueConver;
import com.btcode.orm.conver.IValueConver;
import com.btcode.orm.executer.DefaultORMExecuterFactory;
import com.btcode.orm.executer.IORMExecuter;
import com.btcode.orm.executer.IORMExecuterFactory;

/**
 * @author gislin 2017年9月19日
 */
public class DBManager {

    private static DBManager instance;

    private IDataExecuter dataExecuter;

    private IORMExecuter ormExecuter;

    public static boolean isCache = false;

    private DBManager() {
        initDBManager();
    }

    private void initDBManager() {
        DataBaseConfig dbConfig = new DataBaseConfig();
        dbConfig.setConnectionString("jdbc:oracle:thin:@192.168.1.162:1521:prod");
        dbConfig.setDbName("prod");
        dbConfig.setUserName("OPSZRE");
        dbConfig.setPassword("OPSZRE");
        dbConfig.setDbType(DataBaseType.oracle);
        dbConfig.setDirverClass("oracle.jdbc.driver.OracleDriver");
        IDataExecuterFactory deFactory = new DefaultDataExecuterFactory();
        dataExecuter = deFactory.getDataExecuter(dbConfig);
        dataExecuter.setLog(MyLog.getInstance().getLogger("dbexecuter"));

        IORMCacheExecuter ormCache;

        if (!isCache) {
            ormCache = new EmptyOrmCacheExecuter();
        }
        else {
            ormCache = new EhcacheExecuter();
        }

        IDataExecuter dataExecuter = getDataExecuter();
        IORMExecuterFactory deOrmFactory = new DefaultORMExecuterFactory();
        IValueConver valueCover = new DefaultValueConver();
        ormExecuter = deOrmFactory.getORMExecuter(dataExecuter, new DefaultModelConver(), ormCache,
                                                  valueCover);
    }

    public static DBManager getInstance() {
        if (instance == null) {
            instance = new DBManager();
        }
        return instance;
    }

    public IDataExecuter getDataExecuter() {

        return dataExecuter;
    }

    public IORMExecuter getORMExecuter() {

        return ormExecuter;
    }

}
